package game.resource.loader;

import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;

import common.Log;
import engine.filesystem.FileScanner;
import game.resource.loader.builder.RegularSpriteSheetBuilder;
import game.resource.loader.builder.ResourceBuilder_I;
import game.resource.loader.builder.SpriteBuilder;

public class SaxStreamHandler extends DefaultHandler {
	public SaxStreamHandler() {
	}

	private boolean _AbortParsing;
	
	public ResourceBuilder_I Builder;
	@Override
	public void startDocument() {
		Log.debug("Start of XML document");
	}
	
	@Override
	public void endDocument() {
		Log.debug("End of XML document");
	}

	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) {
		if(_AbortParsing == true) {
			return;
		}
		if(Builder == null) {
			switch(localName.toLowerCase()) {
				case "sprite":
					Builder = new SpriteBuilder();
					break;
				case "spritesheet":
					// TODO: Implement spritesheet builder
					break;
				case "regularsheet":
					Builder = new RegularSpriteSheetBuilder();
					break;
				default:
					Log.warning("Unknown tag : <" + localName + "> aborting XML parsing.");
			}
		}
		if(Builder.pushTag(localName, attributes) == false) {
			Log.debug("Error loading sprite.");
		}
	}
	
	@Override
	public void endElement(String uri, String localName, String qName) {
	}
	
	@Override
	public void characters(char[] ch, int start, int length) {
		String output = new String(ch,start,length).trim();
		if(output.length() > 0) {
			Log.write(output);
		}
	}
}